package com.xuequ.answerme.account;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.View;

import com.xuequ.answerme.R;
import com.xuequ.answerme.main.StoreFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shijie on 2015/11/6.
 */
public class LoginActivity extends AppCompatActivity {

    private CardView cardView;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private TabLoginFragmentAdapter fragmentAdapter;

    private List<String> tabTitlesList;
    private List<Fragment> fragmentsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_join_layout);
        initViews();

        //3s后显示登陆卡片
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //Animation Start
                        cardView.setVisibility(View.VISIBLE);
                    }
                });
            }
        }.start();

    }

    private void initViews() {

        cardView = (CardView) findViewById(R.id.login_join_cardView);
        cardView.setVisibility(View.GONE);

        mTabLayout = (TabLayout) findViewById(R.id.login_join_tabLayout);
        mViewPager = (ViewPager) findViewById(R.id.login_join_viewPager);

        tabTitlesList = new ArrayList<>();
        fragmentsList = new ArrayList<>();

        tabTitlesList.add("LOGIN");
        tabTitlesList.add("JOIN");

        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitlesList.get(0)));
        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitlesList.get(1)));

        fragmentsList.add(new LoginFragment());
        fragmentsList.add(new JoinFragment());

        fragmentAdapter = new TabLoginFragmentAdapter(getSupportFragmentManager(),fragmentsList,tabTitlesList);
        mViewPager.setAdapter(fragmentAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(fragmentAdapter);

    }

    public class TabLoginFragmentAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> mFragments;
        private List<String> mTitles;

        public TabLoginFragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
            super(fm);
            mFragments = fragments;
            mTitles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles.get(position);
        }
    }
}
